/****************************************************************************

 Copyright (C) 2009 Jean Pierre Charalambos. All rights reserved.

 Jean Pierre Charalambos (http://dis.unal.edu.co/profesores/pierre/) is an
 Affiliate Associate Professor of the Faculty of Engineering at National
 University of Colombia (http://www.unal.edu.co/).

 This file is part of maku version 0.1.

 http://code.google.com/p/maku/
 email: jpcharalambosh@unal.edu.co

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the COPYING file included in the packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include <math.h>
#include "decimaterdialog.h"
#include <QPushButton>

DecimaterDialog::DecimaterDialog(DecimaterOptions * opt, QWidget *parent)
     : QDialog(parent) {
     setupUi(this);
     QMetaObject::connectSlotsByName(this);
     decOpt = opt;
     verticesSpinBox->setMaximum( decOpt->maxVertices );
     float val = ((float)decOpt->maxVertices)/2.0;
     verticesSpinBox->setValue( (int) val );
     collapsesSpinBox->setMaximum( decOpt->maxCollapses );
     val = ((float)decOpt->maxCollapses)/2.0;
     collapsesSpinBox->setValue( (int) val );
     stackedWidget->setCurrentIndex(0);
     collapseButton = new QPushButton(tr("&Constraints >>"), buttonBox);
     connect(collapseButton, SIGNAL(released()), this, SLOT(collapseOrExpandDialog()));
     buttonBox->addButton(collapseButton, QDialogButtonBox::ResetRole);
     constraintsGroupBox->setVisible(false);
     resize( QSize(width(), (decimateGroupBox->height() + buttonBox->height())) );
}

void DecimaterDialog::on_percentageButton_toggled(bool percentage) {
  if(percentage)
    stackedWidget->setCurrentIndex(0);
}

void DecimaterDialog::on_verticesButton_toggled(bool vertices) {
  if(vertices)
    stackedWidget->setCurrentIndex(1);
}

void DecimaterDialog::on_collapsesButton_toggled(bool collapses) {
  if(collapses)
    stackedWidget->setCurrentIndex(2);
}

void DecimaterDialog::on_targetCheckBox_toggled(bool checked) {
  if(checked) {
    errorCheckBox->setChecked(true);
    errorCheckBox->setDisabled(true);
  }
  else {
    errorCheckBox->setChecked(false);
    errorCheckBox->setEnabled(true);
  }
}

void DecimaterDialog::collapseOrExpandDialog() {
    int collapseHeight = 0;
    QWidget *widgetToHide = constraintsGroupBox;
    if (widgetToHide->isVisible()) {
        collapseButton->setText(tr("&Constraints >>"));
        collapseHeight = widgetToHide->y() + widgetToHide->height() - (decimateGroupBox->y() + decimateGroupBox->height());
    }
    else
        collapseButton->setText(tr("&Constraints <<"));
    widgetToHide->setVisible(! widgetToHide->isVisible());
    if (! widgetToHide->isVisible()) { // ajuste el tamaño
        layout()->activate();
        resize( QSize(width(), height() - collapseHeight) );
    }
}

void DecimaterDialog::on_buttonBox_accepted() {
  decOpt->hasQ = !targetCheckBox->isChecked();
  //hack para poder hacer binario el modulo de quadrics
  if ( decOpt->hasQ ) {
    decOpt->nPercentage = (float) percentageSpinBox->value();
    decOpt->nVertices = (float) verticesSpinBox->value();
    decOpt->nCollapses = (float) collapsesSpinBox->value();
  }
  else {
    decOpt->nPercentage = 0;
    decOpt->nVertices = 0;
    decOpt->nCollapses = 0;
  }
  decOpt->hasConstraintQ = errorCheckBox->isChecked();
  decOpt->hasNF = normalCheckBox->isChecked();
  decOpt->hasR = roundnessCheckBox->isChecked();
  decOpt->hasIS = setsCheckBox->isChecked();
  decOpt->targetIsPercentage = percentageButton->isChecked();
  decOpt->targetIsVertices = verticesButton->isChecked();
  decOpt->targetIsCollapses = collapsesButton->isChecked();
  decOpt->Q = (float) errorSpinBox->value() / pow( 10, (float) powSpinBox->value() ) ;
  decOpt->NF = normalSpinBox->value();
  decOpt->R = roundSpinBox->value();
}
